package com.example.answer_staff;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gaosai.staff.utils.CommonUtils;
import com.gaosai.staff.utils.JsonUtils;
import com.gaosai.staff.utils.SPUtil;

public class RegisterActivity  extends Activity{
	private String[] enroll;
	private LinearLayout regist_lineraylayout_main;
	private Button activity_register_bt;
	private ArrayList<EditText> edit_array;
	private ArrayList<TextView> textview_array;
	private ArrayList<String> enroll_s_spilt_list;
	private ArrayList<String> register_list_key;
	private ArrayList<String> register_list_value;
	private ImageView activity_back_regist;
	private String barCode_share;
	private String[] barcode_string;
	private ArrayList<TextView> list_yes;
	private String application_list;
	private String Subject;
	private int StartTime;
	private int EndTime;
	private String Venue;
	private TextView Activity_Subject;
	private TextView Activity_StartTime;
	private TextView Activity_EndTime;
	private TextView Activity_Venue;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		//屏蔽输入法弹出框
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
		setContentView(R.layout.regist);
		setupView();
		geographicallayout();
		setListeners();
	}
	private void geographicallayout() {
		regist_lineraylayout_main=(LinearLayout) this.findViewById(R.id.regist_lineraylayout_main);
		regist_lineraylayout_main.removeAllViews();
		for( int  i=0;i<enroll.length;i++){
			LinearLayout textview_linearlayout=(LinearLayout) LayoutInflater.from(this).inflate(R.layout.regist_textview_dynamic, regist_lineraylayout_main,false);
			TextView tv_first = (TextView)textview_linearlayout.findViewById(R.id.tv_first);
			textview_array.add(tv_first);
			TextView tv_second=(TextView)textview_linearlayout.findViewById(R.id.tv_second);
			String enroll_s = enroll[i];
			String[] enroll_s_spilt = enroll_s.split(":");
			enroll_s_spilt_list.add(enroll_s_spilt[1]);
			tv_first.setText(""+enroll_s_spilt[0]);
			tv_second.setText("*");
			if(enroll_s_spilt[1].equals("yes")){
				tv_second.setTextColor(Color.RED);
			}else{
				tv_second.setTextColor(Color.GREEN);
			}
			list_yes.add(tv_second);
			EditText et = (EditText) LayoutInflater.from(this).inflate(R.layout.regist_editext_dynamic,regist_lineraylayout_main,false);
			edit_array.add(et);
			Set(et,i);
			regist_lineraylayout_main.addView(textview_linearlayout);
			regist_lineraylayout_main.addView(et);
		}
	}
	//将扫描二维码得到的数据显示出来：staffId english_anme chinese_name 等等
	private void Set(EditText et, int i) {
		if(i==0){
			et.setText(""+barcode_string[0]);
			et.setEnabled(false);
		    et.setTextColor(Color.BLACK);
		}else if(i==1){
			et.setText(""+barcode_string[2]);
			et.setEnabled(false);
			  et.setTextColor(Color.BLACK);
		}else if(i==2){
			et.setText(""+barcode_string[3]);
			et.setEnabled(false);
			  et.setTextColor(Color.BLACK);
		}else if(i==3){
			et.setText(""+barcode_string[4]);
			et.setEnabled(false);
			  et.setTextColor(Color.BLACK);
		}else{
			return;	
		}
	}
	private void setupView() {
		list_yes=new ArrayList<TextView>();
		activity_register_bt=(Button)findViewById(R.id.activity_regist_bt);
		application_list=getIntent().getStringExtra("application_list");
		activity_back_regist=(ImageView)findViewById(R.id.activity_back_regist);
		jsonParse(application_list);
		//应用程序如果一卸载，这个值就取不到了，所以应该将它放在sd卡里边 SPUtil.BARCODE+barcode_string[1]
		//		barCode_share = SPUtil.getValue(getApplicationContext(),SPUtil.BARCODE+SPUtil.getValue(getApplicationContext(), SPUtil.UserName));
		Log.i("eee", "readFromMap的name是"+SPUtil.BARCODE+SPUtil.getValue(getApplicationContext(), SPUtil.UserName));
		barCode_share= CommonUtils.readFromMap(CommonUtils.getUserInfo(getApplicationContext(), SPUtil.BARCODE+SPUtil.getValue(getApplicationContext(), SPUtil.UserName)), "user_info_list"+SPUtil.BARCODE+SPUtil.getValue(getApplicationContext(), SPUtil.UserName));
		CommonUtils.getUserInfo2(getApplicationContext());
		Log.i("eee", "从sd卡取出的barCode_share是"+barCode_share);
		Log.i("eee", "从sd卡取出的username是"+SPUtil.UserName);
		Log.e("eee","解析-------barcode+userName是"+SPUtil.BARCODE+SPUtil.getValue(getApplicationContext(), SPUtil.UserName));
		Log.e("1111", "barCode_share是"+barCode_share);
		barcode_string=JsonUtils.ParseQr(barCode_share);
		Log.e("eee", "barcode_string是"+barcode_string);
		edit_array=new ArrayList<EditText>();
		enroll_s_spilt_list=new ArrayList<String>();
		textview_array=new ArrayList<TextView>();
		register_list_key=new ArrayList<String>();
		register_list_value=new ArrayList<String>();
		Activity_Subject=(TextView)findViewById(R.id.activity_register_subject);
		Activity_StartTime=(TextView)findViewById(R.id.activity_register_start_time);
		Activity_EndTime=(TextView)findViewById(R.id.ativity_register_end_time);
		Activity_Venue=(TextView)findViewById(R.id.Activity_register_Venue);
		Activity_Subject.setText(Subject);
		Activity_StartTime.setText(CommonUtils.getDateToStringt(StartTime));
		Activity_EndTime.setText(CommonUtils.getDateToStringt(EndTime));
		Activity_Venue.setText(Venue);
		Log.i("eee", "register中得到的enroll的长度是"+enroll.length);
	}
	private void jsonParse(String  json) {
		JSONObject jsonObject=null;
		try {
			jsonObject = new JSONObject( json);
			Subject=jsonObject.getString("subject");
			StartTime=jsonObject.getInt("startTime");
			EndTime=jsonObject.getInt("endTime");
			Venue=jsonObject.getString("venue");
			String enroll_need = jsonObject.getString("enroll_need");
			Spilt(enroll_need);
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	private void Spilt(String string) {
		enroll = string.split("==");
		Log.i("eee", "liatadapter中得enroll的长度是"+enroll.length);
	}
	private void setListeners() {
		//注册按钮的监听事件
		activity_register_bt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				register_list_key.clear() ;
				register_list_value.clear() ;
				for(int i=0;i<enroll.length;i++){
					String boll = enroll_s_spilt_list.get(i);
					Log.i("eee", "boll是"+boll);		
					TextView tv_arr = textview_array.get(i);
					String key = tv_arr.getText().toString();
					Log.i("eee", "key是"+key);
					EditText et_arr = edit_array.get(i);
					String  value = et_arr.getText().toString();
					register_list_key.add(key);
					register_list_value.add(value);
					Log.i("eee", "valet_value是"+value);
					//判断带*号的是必填项
					if(list_yes.get(i).getTextColors().equals(ColorStateList.valueOf(Color.RED))){
						if(value.equals("")){
							Toast.makeText(getApplicationContext(), key+"为必填项", 1).show();
							return;
						}
					}
				}
				Intent intent=new Intent(RegisterActivity.this,VerifyActivity.class);
				intent.putExtra("application_list", application_list);
				intent.putExtra("register_list_key",register_list_key);
				intent.putExtra("register_list_value",register_list_value);
				System.out.println("");
				startActivity(intent);
			}
		});
		activity_back_regist.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
	}















}
